<?php 
class Upreg extends Controller {
	function Upreg() {
		parent::Controller();
		$this->load->helper("url");
		$this->data["base"] = $this->config->item('base_url');
		$this->load->model('Sysadminmodel','',TRUE);
		$this->load->model('Mail');
		
		$this->load->database();	
                session_start();

	}
	
	function index()  {
			$this->data['title'] = 'User Registration';
			$this->data['h2_title'] = ' ';
			$this->data['form_action'] =site_url('upreg/upreg/upreg_send');
			$this->data['send_id'] = $this->Sysadminmodel->show_id_user();
			$this->data['dept'] =$this->Sysadminmodel->show_dept();
			$this->load->vars($this->data);
			$this->load->view('layout/head');
			$this->load->view('layout/body');
			$this->load->view('layout/slice-atas');
			$this->load->view('upreg/upreg');
			$this->load->view('layout/slice_bawah');
			
			
	}
	function upreg_send() {
			$this->data['h2_title'] = ' Send Register User';
			$this->data['form_action'] =site_url('upreg/upreg/upreg_send');
			$this->data['send_id'] = $this->Sysadminmodel->show_id_user();
			
			$this->data['dept'] =$this->Sysadminmodel->show_dept();
			
			$this->form_validation->set_rules('name', 'Name', 'required|callback_valid_entry');
			$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
			$pass2 = md5($this->input->post('name'));
			if($this->form_validation->run() == TRUE) {
			
			$upreg = array('user_id' =>$this->input->post('user_id'),
							'name' =>$this->input->post('name'),
							'email' =>$this->input->post('email'),
							'pass' =>$this->input->post(''),
							'pass2' =>$this->input->post(''),
							'dept_id' =>$this->input->post('dept_id'),
							'mandiri_id' =>$this->input->post(''),
							'status' =>$this->input->post(''),
							'user_right' =>$this->input->post('user_right'));
			// insert db user
			$user_id = $this->input->post('user_id');
			$this->Sysadminmodel->insert_upreg($upreg);
				$this->session->set_flashdata('message', 'Register Success !');
				echo " <script>alert('Untuk mengaktifkan akun cek mail dan tunggu pesan verifikasi dari Sysadmin ');</script>";
							}
			else {
				$this->session->set_flashdata('message', 'Chek phrase !');
			}
			//redirect('upreg/upreg');
			$this->load->vars($this->data);
			$this->load->view('layout/head');
			$this->load->view('layout/body');
			$this->load->view('layout/slice-atas');
			$this->load->view('upreg/upreg');
			$this->load->view('layout/slice_bawah');
			
		
	}
	  function upreg_verif() {
	  		$this->data['title'] ='User Verification';
	  		$ao ="0";
	  		$ver = $this->Sysadminmodel->show_user_upreg($ao)->result();
			
			//set template table untuk efek selang-sling tiap baris
			$tmpl = array('table_open' =>'<table border="0" cellpadding="0" cellspacing="0" width="560">','row_alt_start' =>'<tr class="tb-jd">','row_alt_end' =>'</tr>');
			$this->table->set_template($tmpl);
			
			//set heading untul tabel
			$this->table->set_empty("&nbsp;");
			$this->table->set_heading('No','Name','Email','Dept Name','Actions');
			
			$i = 0;
			foreach($ver as $veri) {
				$this->table->add_row(++$i,$veri->name,$veri->email,$veri->dept_name,
				anchor('upreg/upreg/verif/'.$veri->user_id,'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',array('class'=>'verifikasi')));
			}
			
			$this->data['table'] = $this->table->generate();
			
						
			
			$this->load->vars($this->data);
			$this->load->view('layout/head');
			$this->load->view('layout/body');
			$this->load->view('layout/slice-atas');
			$this->load->view('sysadmin/message');
			$this->load->view('layout/slice_bawah');
			
			
	  }
	  function verif($id_verif) {
	  		
	  		$xp = $this->Sysadminmodel->show_upreg_data ($id_verif)->row();
			$verif = array('pass2' =>md5($xp->name));
			// inset verif
			$id = $xp->user_id;
			$this->Sysadminmodel->update_verif($id,$verif);
			
			// send mail 
			$this->load->library('email');
			$config['charset'] = 'iso-8859-1';
			$config['wordwrap'] = FALSE;
			$this->email->initialize($config);
			
			$from_mail = 'gunk53@gmail.com'; // --> or $this->input->post('from_mail');
			$from_name = 'Adnan Tawaffani'; // --> or $this->input->post('from_name');
			$to = 'adnan@adnantawaffani.net';
			$cc = 'support@hostingjitu.com,billing@hostingjitu.com';
			$subject = 'Konfirmasi Email Codeigniter'; // --> or $this->input->post('subject');
			$message = 'Wah, ternyata Codeigniter bagus loh.. Nyobain deh.. Ntar Jadi ketagihan..'; // --> or $this->input->post('message');
			
			$this->email->from($from_mail,$from_name);
			$this->email->to($to);
			$this->email->cc($cc);
			$this->email->subject($subject);
			$this->email->message($message);
			$this->email->send();
			
			$this->email->print_debugger();
			
			$this->email->from($from_mail,$from_name);
			$this->email->to($to);
			$this->email->cc($cc);
			$this->email->subject($subject);
			$this->email->message($message);
			$this->email->send();
	
			$this->email->print_debugger();
			
			
			$this->session->set_flashdata('message', 'Register Success !');
			
			redirect('upreg/upreg/upreg_verif');
	  }
	  
	  function valid_entry()
		{
			$name 		= $this->input->post('name');
		
		
			if($this->Sysadminmodel->valid_entry($name) == FALSE)
			{
				$this->form_validation->set_message('valid_entry', 'Username sudah ada ' );
				return FALSE;
			}
			else
			{
				return TRUE;
			}
	}
}
?>